package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.UserTagAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.UserTagBean;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectTagActivity extends BaseActivity {

    @BindView(R.id.rv_mettle_tag)
    RecyclerView rvMettleTag;
    @BindView(R.id.rv_interest_tag)
    RecyclerView rvInterestTag;
    @BindView(R.id.tv_next_step)

    TextView tvNextStep;
    private String tags;
    private List<UserTagBean.ListBean> tagData1;
    private List<UserTagBean.ListBean> tagData2;
    private UserTagAdapter adapter1;
    private UserTagAdapter adapter2;
    private List<String> mettleTags;
    private List<String> interestTags;

    public static void actionStar(Context context, String tags) {
        Intent intent = new Intent(context, SelectTagActivity.class);
        intent.putExtra("tags", tags);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_select_tag;
    }

    @Override
    protected void initView() {
        setTitleText("个人标签");
        tags = getIntent().getStringExtra("tags");
        mettleTags = new ArrayList<>();
        interestTags = new ArrayList<>();
        initRecyclerView();
        getTagData();
    }

    private void getTagData() {

        OkGoUtil.postRequest(getContext(), UrlConstants.GET_TAGS, null, new JsonCallback<BaseModel<UserTagBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<UserTagBean>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        tagData1.addAll(response.body().getData().getList());
                        tagData2.addAll(response.body().getData().getList1());
                        synData();
                    }
                }
            }

            @Override
            public void onError(Response<BaseModel<UserTagBean>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    //同步标签数据
    private void synData() {
        if(StringUtils.isEmpty(tags)){
            adapter1.notifyDataSetChanged();
            adapter2.notifyDataSetChanged();
            return;
        }

        String[] myTags = tags.split(",");

        for(int i = 0;i<tagData1.size();i++){
            for(int j = 0;j<myTags.length;j++){
                if(tagData1.get(i).getTitle().equals(myTags[j])){
                    mettleTags.add(tagData1.get(i).getTitle());
                    tagData1.get(i).setSelected(true);
                }
            }
        }

        for(int i = 0;i<tagData2.size();i++){
            for(int j = 0;j<myTags.length;j++){
                if(tagData2.get(i).getTitle().equals(myTags[j])){
                    interestTags.add(tagData2.get(i).getTitle());
                    tagData2.get(i).setSelected(true);
                }
            }
        }
        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();
    }

    private void initRecyclerView() {
        tagData1 = new ArrayList<>();
        tagData2 = new ArrayList<>();
        adapter1 = new UserTagAdapter();
        adapter2 = new UserTagAdapter();
        //标签流式布局
        FlexboxLayoutManager historyManager1 = new FlexboxLayoutManager(context);
        historyManager1.setFlexWrap(FlexWrap.WRAP); //设置是否换行
        historyManager1.setFlexDirection(FlexDirection.ROW); // 设置主轴排列方式
        historyManager1.setAlignItems(AlignItems.STRETCH);
        historyManager1.setJustifyContent(JustifyContent.FLEX_START);
        rvMettleTag.setLayoutManager(historyManager1);
        //标签流式布局
        FlexboxLayoutManager historyManager2 = new FlexboxLayoutManager(context);
        historyManager2.setFlexWrap(FlexWrap.WRAP); //设置是否换行
        historyManager2.setFlexDirection(FlexDirection.ROW); // 设置主轴排列方式
        historyManager2.setAlignItems(AlignItems.STRETCH);
        historyManager2.setJustifyContent(JustifyContent.FLEX_START);
        rvInterestTag.setLayoutManager(historyManager2);


        rvMettleTag.setAdapter(adapter1);
        rvInterestTag.setAdapter(adapter2);
        adapter1.setNewData(tagData1);
        adapter2.setNewData(tagData2);

        adapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (tagData1.get(position).isSelected()) {
                    tagData1.get(position).setSelected(false);
                    mettleTags.remove(tagData1.get(position).getTitle());
                } else {
                    if (mettleTags.size() < 2) {
                        mettleTags.add(tagData1.get(position).getTitle());
                        tagData1.get(position).setSelected(true);
                    }
                }
                adapter1.notifyDataSetChanged();
            }
        });

        adapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (tagData2.get(position).isSelected()) {
                    tagData2.get(position).setSelected(false);
                    interestTags.remove(tagData2.get(position).getTitle());
                } else {
                    if (interestTags.size() < 2) {
                        interestTags.add(tagData2.get(position).getTitle());
                        tagData2.get(position).setSelected(true);
                    }
                }
                adapter2.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    @OnClick(R.id.tv_next_step)
    public void onClick() {
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<mettleTags.size();i++){
            sb.append(mettleTags.get(i)).append(",");
        }
        for(int i = 0;i<interestTags.size();i++){
            sb.append(interestTags.get(i)).append(",");
        }
        if(sb.length() == 0){
            ToastUtil.showToast(context,"未选择标签");
            return;
        }

        sb.deleteCharAt(sb.length()-1);
        Map map = new HashMap();
        map.put("tags",sb.toString());
        OkGoUtil.postRequest(getContext(), UrlConstants.EDIT_USER_DATA, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        finish();
                    }
                    ToastUtil.showToast(context,response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }
}